# -*- coding: utf-8 -*-

###########################################################################
#
#   Copyright (c) 2020  Bresee, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###########################################################################

### Channel operator management  渠道运营商管理


from flask import Blueprint
from flask import request
import utils.wraps.urlcost as urlcost
import utils.wraps.token as token
import utils.message.response as response
import utils.pgsql.pgchannel
import utils.wraps.token
import json



channel_module = Blueprint(
    'channel', __name__, url_prefix='/api/v1/account/channel')


@channel_module.route('/', methods=['POST'])
@token.login_required
@urlcost.cost_count_wraps
def channel_add():
 
    userInfo = token.getUserInfoFromAuth(request.headers["Authorization"])

    if userInfo['role'] == 1:
        obj = utils.pgsql.pgchannel.PgChannel()
        channelInfo = json.loads(request.data)

        res = obj.addChannel(channelInfo)
        if res == {}:
            return response.make_response_1505()
        return response.make_response_success(res)
    else:
        return response.make_response_401()


@channel_module.route('/', methods=['GET'])
@token.login_required
@urlcost.cost_count_wraps
def channel_all():
 
    userInfo = token.getUserInfoFromAuth(request.headers["Authorization"])
    if userInfo['role'] == 1:
        obj = utils.pgsql.pgchannel.PgChannel()
        res = obj.all()
        if res == '':
            return response.make_response_1505()
        return response.make_response_success(res)
    else:
        return response.make_response_401()


@channel_module.route('/unused', methods=['GET'])
@token.login_required
@urlcost.cost_count_wraps
def channel_id():
 
    userInfo = token.getUserInfoFromAuth(request.headers["Authorization"])

    if userInfo['role'] == 1:
        obj = utils.pgsql.pgchannel.PgChannel()
        res = obj.getUnusedChannel()
        if res == '':
            return response.make_response_1505()
        return response.make_response_success(res)
    else:
        return response.make_response_401()


@channel_module.route('/<int:channelId>', methods=['DELETE'])
@token.login_required
@urlcost.cost_count_wraps
def channel_delete(channelId):
     
    userInfo = token.getUserInfoFromAuth(request.headers["Authorization"])

    if userInfo['role'] == 1:
        obj = utils.pgsql.pgchannel.PgChannel()
        if obj.delete(channelId):
            return response.make_response_success_no_data()
        else:
            return response.make_response_1000()
    else:
        return response.make_response_401()
